//
//  editMyResumeAddOREditEdu.swift
//  DingDongZP
//
//  Created by Zhongnan on 16/5/8.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

enum LDEditMyResumeAddOREditEduType:String{
    case Add = "add"
    case Edit = "edit"
}

class LDEditMyResumeAddOREditEdu: LDBaseCustomNavTabVC {
    
    let titleArr = ["学校名称","专业名称","学历","时间段"]
    
    var cellContentdict = [Int:String]()
    var enterButton:UIButton?
    var deleButton:UIButton?
    
    var eduModel:LDMyResumeEduexperienceModel?
    
    var bottomView:UIView?
    var type:LDEditMyResumeAddOREditEduType = .Add
    
    //选择日期
    var timePicker:LDBaseTimePicker?
    var timeVC:LDBaseTimeSelVC?
    //学历
    var actionView:popActionSheetView?
    //
    var sendParams = [String:String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setDefaultContent()
        
    }
    
    func setDefaultContent() {
        title = "添加教育经历"
        tableView = LDBaseTableView.init(frame: view.bounds, style: UITableViewStyle.Plain)
        
        timeVC = LDBaseTimeSelVC()
        addChildViewController(timeVC!)
        
        if type == .Edit {
            
            cellContentdict[0] = eduModel?.institutename
            cellContentdict[1] = eduModel?.majortitle
            cellContentdict[221] = eduModel?.eduid
            cellContentdict[2] = eduModel?.edu
            
            cellContentdict[3] = eduModel!.startdate + "-" + eduModel!.enddate //eduInfoDict["enddate"]!
        }
        
        setBottomBtn()
    }
    
    func deleEduExp() {
        
        let eduexpid = eduModel?.eduexperienceid
        LDUserInfoNet.sharedInstance.deleteEduExp(eduexpid, success: { [weak self] (value) in
            
            LDUserInfo.sharedInstance.updataJobFinderMyResume(nil, failure: nil)
            LDShowHUB.shareInstance.showSucc(value as! String)
            self?.navigationController?.popViewControllerAnimated(true)
        }) { (data) in
            LDShowHUB.shareInstance.showErr(data as! String)
        }
    }
    
    func EditEduExp() {
        
        sendParams["institute_name"] = cellContentdict[0]
        sendParams["major_title"] = cellContentdict[1]
        sendParams["edu_id"] = cellContentdict[221]
        
        let start = cellContentdict[3]?.componentsSeparatedByString("-").first
        let end = cellContentdict[3]?.componentsSeparatedByString("-").last
        
        sendParams["startdate"] = getUnixDateFormString(start!)
        sendParams["enddate"] = getUnixDateFormString(end!)
        sendParams["eduexperience_id"] = eduModel?.eduexperienceid
        
        let api = LDJobfinderAPI.edit.eduexperience
        LDNetWorkManager.sharedInstance.LoginPOSTSET(api, parameters: sendParams, success: { [weak self] (value) in
            
            LDShowHUB.shareInstance.showSucc(value as! String)
            LDUserInfo.sharedInstance.updataJobFinderMyResume(nil, failure: nil)
            self?.navigationController?.popViewControllerAnimated(true)
            
        }) { (data, error) in
            
            LDShowHUB.shareInstance.showErr(data as! String)
        }
    }
    
    func AddEduExp() {
        
        if cellContentdict.count < 4 {
            showLDAlertView("信息未填写完整")
            return
        }
        
        sendParams["institute_name"] = cellContentdict[0]
        sendParams["major_title"] = cellContentdict[1]
        sendParams["edu_id"] = cellContentdict[221]
        
        guard let start = cellContentdict[3]?.componentsSeparatedByString("-").first else { LDShowHUB.shareInstance.showErr("信息未填写完整"); return }
        guard let end = cellContentdict[3]?.componentsSeparatedByString("-").last else {LDShowHUB.shareInstance.showErr("信息未填写完整"); return}
        
        sendParams["startdate"] = getUnixDateFormString(start)
        sendParams["enddate"] = getUnixDateFormString(end)
        
        
        if sendParams["institute_name"] == nil {
            
            LDShowHUB.shareInstance.showErr("信息未填写完整")
            return
        }
        if sendParams["major_title"] == nil {
            
            LDShowHUB.shareInstance.showErr("信息未填写完整")
            return
        }
        if sendParams["edu_id"] == nil {
            
            LDShowHUB.shareInstance.showErr("信息未填写完整")
            return
        }
        if sendParams["startdate"] == nil {
            
            LDShowHUB.shareInstance.showErr("信息未填写完整")
            return
        }
        
        let api = LDJobfinderAPI.create.eduexperience
        LDNetWorkManager.sharedInstance.LoginPOSTSET(api, parameters: sendParams, success: {
            [weak self] (value) in
            
            LDShowHUB.shareInstance.showSucc(value as! String)
            LDUserInfo.sharedInstance.updataJobFinderMyResume(nil, failure: nil)
            self?.navigationController?.popViewControllerAnimated(true)
            
        }) { (data, error) in
            
            LDShowHUB.shareInstance.showErr(data as! String)
        }
    }
    
    func setBottomBtn() {
        
        bottomView = UIView.init(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, 100))
        bottomView?.backgroundColor = LDGolbalColor.BgGoundGray
        tableView.tableFooterView = bottomView
        
        
        enterButton = UIButton.setDetailsBtn("保存", picName: nil, btnBackColor: LDGolbalColor.Green, titleColor: LDFontColor.White)
        deleButton = UIButton.setDetailsBtn("删除", picName: nil, btnBackColor: LDGolbalColor.Red, titleColor: LDFontColor.White)
        
        bottomView!.addSubview(deleButton!)
        bottomView!.addSubview(enterButton!)
        
        enterButton?.bk_whenTapped({ [weak self] in
            
            if self?.type == .Add {
                
                self?.AddEduExp()
                
            } else {
                
                self?.EditEduExp()
            }
        })
        
        deleButton?.bk_whenTapped({ [weak self] in
            self?.deleEduExp()
        })
        
        if type == .Add {
            deleButton?.hidden = true
            enterButton?.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(25)
                make.right.equalTo(-10)
                make.left.equalTo(10)
                make.height.equalTo(45)
            })
        }
        
        if type == .Edit {
            deleButton?.hidden = false
            enterButton?.snp_makeConstraints(closure: { (make) in
                
                make.left.equalTo((bottomView?.snp_centerX)!).offset(5)
                make.top.equalTo(25)
                make.right.equalTo(-10)
                make.height.equalTo(45)
            })
            
            deleButton?.snp_makeConstraints(closure: { (make) in
                
                make.top.height.equalTo(enterButton!)
                make.left.equalTo(10)
                make.right.equalTo((bottomView?.snp_centerX)!).offset(-5)
                
            })
        }
    }
}
extension LDEditMyResumeAddOREditEdu {
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 
        return titleArr.count
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCellWithIdentifier("addJobCell") as? baseMySettingCells
        if cell == nil {
            cell = baseMySettingCells.init(style: .Default, reuseIdentifier: "addJobCell")
        }
        cell?.titleLab?.text = titleArr[indexPath.row]
        cell?.descLab?.text = cellContentdict[indexPath.row]
        return cell!
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 55
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let index = indexPath.row
        
        if index == 0 || index == 1 {
            
            creatTitleVC(indexPath.row)
            
        } else if indexPath.row == 2 {
            
            LDCheckPublicInfo.shareInstance.checkoutAllMainEduInfoJobfinderEdit({ [weak self] (data) in
                
                var titleArr = [String]()
                var title_IDArr = [String]()
                for item in data {
                    
                    titleArr.append(item["edu_text"] as! String)
                    title_IDArr.append(item["edu_id"] as! String)
                }
                
                self?.actionView = popActionSheetView()
                self?.actionView!.selectSheetTextArr = titleArr
                self?.actionView?.btnH = 25
                self?.actionView?.cancelBtnH = 30
                self?.actionView?.showHeadView = true
                
                self?.actionView?.selectOfRows({ [weak self] (title, index) in
                    self?.cellContentdict[2] = title
                    self?.cellContentdict[221] = String(index)
                    self?.tableView.reloadData()
                })
                
                let rootvc = UIApplication.sharedApplication().keyWindow?.rootViewController?.childViewControllers[0]
                self?.actionView!.showview(rootvc!.view)
            })
            
        } else if indexPath.row == 3 {
            
            timePicker = timeVC!.timePicker
            timePicker!.showview(self.view)
            timeVC!.sendDateTimeClosure({ [weak self] (data) in
                self?.cellContentdict[3] = data
                self?.tableView?.reloadData()
            })
        }
    }
    
    func creatTitleVC(index:Int) {
        
        let vc = LDBaseSetTitleVCViewController()
        vc.title = titleArr[index]
        
        if cellContentdict[index] != nil {
            vc.textStr = cellContentdict[index]!
        }
        
        vc.getTitleFromColsure { [weak self] (titles) in
            
            self?.cellContentdict[index] = titles
            self?.tableView?.reloadData()
            self?.navigationController?.popViewControllerAnimated(true)
        }
        
        navigationController?.pushViewController(vc, animated: true)
    }
}
